/**
 * Created by FreezeSoul on 2016/3/27.
 */
define([
    "app/core/base",
    "app/core/util",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel',
    'iziModal'
], function (Chart, Util, $, _, JSON, log) {

    var property = {
        config: "property",
        option: [
            {
                group: '基础',
                id: 'Control_Describe',
                name: '描述说明',
                enable: true,
                type: 'text',
                value: '',
                default: '',
                link: 'describe',
                tooltip: '控件描述说明'
            },
            {
                group: '基础',
                id: 'Control_Every',
                name: '是否每次新建',
                enable: false,
                type: 'checked',
                value: false,
                default: false,
                link: 'every',
                tooltip: '是否每次新建，对于打开多个不同页面适用'
            }
        ],
        MapOption: function (option) {
            Util.linkOption(this.option, option);
        }
    };

    var modal = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._element = null;
            this._container = null;
            this._isLoaded = false;
            this._modalId = "modal-" + Util.newUUID();
            this._option = {
                Series: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {
                    describe: "",
                    every: false
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _render: function () {
            $("#" + this._modalId).remove();
            var $element = $('<div id="' + this._modalId + '"></div>');
            $element.appendTo("body");
            $element.css({"opacity": 0});
            return $element;
        },
        _bindEvent: function () {
            var _this = this;
            if (this._layout.emitter) {
                // 使用介绍：http://izimodal.marcelodolce.com/
                /*
                {
                    title: '',
                    subtitle: '',
                    headerColor: '#88A0B9',
                    theme: '',  // light
                    appendTo: '.body', // or false
                    icon: null,
                    iconText: null,
                    iconColor: '',
                    rtl: false,
                    width: 600,
                    top: null,
                    bottom: null,
                    borderBottom: true,
                    padding: 0,
                    radius: 3,
                    zindex: 999,
                    iframe: false,
                    iframeHeight: 400,
                    iframeURL: null,
                    focusInput: true,
                    group: '',
                    loop: false,
                    navigateCaption: true,
                    navigateArrows: true, // Boolean, 'closeToModal', 'closeScreenEdge'
                    history: false,
                    restoreDefaultContent: false,
                    autoOpen: 0, // Boolean, Number
                    bodyOverflow: false,
                    fullscreen: false,
                    openFullscreen: false,
                    closeOnEscape: true,
                    closeButton: true,
                    overlay: true,
                    overlayClose: true,
                    overlayColor: 'rgba(0, 0, 0, 0.4)',
                    timeout: false,
                    timeoutProgressbar: false,
                    pauseOnHover: false,
                    timeoutProgressbarColor: 'rgba(255,255,255,0.5)',
                    transitionIn: 'comingIn',
                    transitionOut: 'comingOut',
                    transitionInOverlay: 'fadeIn',
                    transitionOutOverlay: 'fadeOut',
                    onFullscreen: function(){},
                    onResize: function(){},
                    onOpening: function(){},
                    onOpened: function(){},
                    onClosing: function(){},
                    onClosed: function(){}
                }
                */
                /*
                if (this._layout){
                    this._layout.emitEvent("showModal", [{
                        iframe: true,
                        iframeHeight: 800,
                        iframeURL: "http://www.baidu.com"
                    }]);
                }
                * */
                if (!this._showModalHandle) {
                    this._showModalHandle = function (params) {
                        _this.showModalHandle.call(_this, params);
                    };
                }
                this._layout.addListener("showModal", this._showModalHandle);
            }
        },
        showModalHandle: function (option) {
            var _this = this;
            var newOption = $.extend(true, {
                radius: 10,
                width: 800,
                iframeHeight: 600,
                onOpening: function () {
                    _this._element.css({"opacity": 1});
                },
                onOpened: function () {
                },
                onClosing: function () {
                },
                onClosed: function () {
                    _this._element.css({"opacity": 0});
                }
            }, option);
            if (_this._isLoaded === false) {
                _this._isLoaded = true;
                _this._element.iziModal(newOption);
                _this._element.iziModal('open');
            } else {
                if (_this._option.Option.every) {
                    _this._element.iziModal('destroy');
                    _this._element.iziModal(newOption);
                }
                _this._element.iziModal('open');
            }
        },
        example: function () {
            try {
                this._element = this._render();
                this._bindEvent();
            } catch (e) {
                log.error(e);
            }
        },
        setData: function () {
            try {
                this._readProperty();
                this._element = this._render();
                this._bindEvent();
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                $(this._element).off().remove();
                this._layout.removeListener("showModal", this._showModalHandle);
            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._element;
        },
        getChart: function () {
            return this._element;
        }
    });

    return {
        Chart: modal,
        Property: property
    };
});